#  通过 Selenium 模拟浏览器抓取，滑动进度条，抓取百度图片
from selenium import webdriver
import time
driver_path = r'E:\Application\chromedriver.exe'
driver = webdriver.Chrome(executable_path=driver_path)
driver.implicitly_wait(20)  # 隐形等待，最长等待20秒
driver.get('https://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&sf=1&fmq=&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&fm=index&pos=history&word=%E7%BE%8E%E5%A5%B3')
time.sleep(4)
# 通过page_source获取网页源代码
# print(driver.page_source)
# 滚动进度条30次，根据你的需要
for i in range(0, 30):
    driver.execute_script('window.scrollTo(0,document.body.scrollHeight);')
    time.sleep(1)
# 将网页内容读取保存至本地
htmlText = driver.page_source
with open(f"resource\source.html", 'w', encoding='utf-8') as f:
    f.write(htmlText)
time.sleep(10)
# driver.close()  # 关闭当前页面
driver.quit()  # 关闭浏览器
